Router.init({
    mode: 'hash',
    root: CONFIG.basePath
});


Router.add({
    path: /(.*)/, //可以使用正则表达式，每一个()匹配将会作为参数传入enterHandler、handler和exitHandler
    params: [],  //初始参数，enterHandler、handler和exitHandler的参数包含params和path中匹配的参数
    enterHandler: function () {
        //路由进入时触发的回调，如果返回false，则handler不触发
        Loader.load() ;

    },
    handler: function () {
        //路由回调
        console.log('do about');
    },
    exitHandler: function () {
        //路由退出时触发的回调
        console.log('exit about');
    }
});

Router.listen();


var Loader = function () {
    var content = $('#content') ;


    var _dmap = {}
    var dialog = function(url){

        var myModalTrigger = new $.zui.ModalTrigger({
            type:'ajax',
            url:url
        });
        _dmap[url] = myModalTrigger ;
        myModalTrigger.show()
        return myModalTrigger
    }

    return {
        load:function () {
            var hash = location.hash.substring(1) ;
            var url = Router.root + hash ;
            if(hash){
                var content = $('#content') ;
                // $.get(url,function (data) {
                //     content.html(data)
                // })
                $.ajax({
                    url:url,
                    type: "GET",
                    success: function (data) {
                        content.html(data)
                    },
                    error:function (xhr,response,err) {
                          App.errorHander(xhr,response,err)
                    }
                });
            }
        },
        go:function(path){
            Router.go(path)
        },
        dialog:dialog,
        getDialog:function(name){
            return _dmap[name]
        }
    }
}()


var App = function(){

    //{hash:'#test',title:'测试'}
    var menus = [] ;

    var silder = new Vue({
        el:'#silder',
        data:{
            menus:menus
        },
        methods:{
            menuClick:function(v){
                if(!v.children){

                    for(var i = 0 ; i < this.menus.length ; i ++ ){
                        this.menus[i].active = false
                        if(this.menus[i].children){
                            var child = this.menus[i].children
                            for (var j = 0 ; j < child.length ; j++){
                                child[j].active = false
                            }
                        }
                    }
                    v.active = true

                    breadcrumb.breadcrumb = v.title
                }

            }
        }
    })

    zhttp.get('rbac/resource/all').then(function (data) {

        for (var j = 0 ; j < data.length ; j++) {
            var d = data[j] ;
            if(d.pid != 0){
                continue;
            }
            var menu =  {hash:'#'+d.link , title:d.name , active:false}
            var subMenu = []
            for(var i = 0 ; i< data.length ; i++){
                if(d.id  == data[i].pid){
                    subMenu.push({hash:'#'+data[i].link , title:data[i].name , active:false})
                }
            }

            if(subMenu.length > 0){
                menu.children = subMenu ;
            }
            silder.menus.push(menu)
        }

    })

    var breadcrumb = new Vue({
        el:'#breadcrumb',
        data:{
            breadcrumb:'Home'
        },
        methods:{
        }
    })

   return {
       silder:silder,
       url:function(){
           return $.url(location.hash.substring(1)) ;
       },
       errorHander:function (xhr,response,err) {
            if(xhr.status == 403){
                error("没有权限") ;
            }
       }
   }
}()



function go(path){
    Loader.go(path)
}
